home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / 4_0 / GNUUCP_2 / SOURCE / LOCKING.C < prev    next >
Text File  |  1990-07-16  |  2KB  |  78 lines

  1. /*
  2.  * @(#)locking.c 1.2 87/09/29    Copyright 1987 Free Software Foundation, Inc.
  3.  *
  4.  * Copying and use of this program are controlled by the terms of the
  5.  * GNU Emacs General Public License.
  6.  */
  7.  
  8. #ifndef lint
  9. char locking_version[] = "@(#)locking.c gnuucp Version Fort Pond Research-2.6";
  10. #endif
  11.  
  12. #include "includes.h"
  13. #include "uucp.h"
  14.  
  15. extern int errno;
  16.  
  17. /*
  18.  * Lock the serial line, if on a multitasking system.
  19.  */
  20. static char    lockname[NAMESIZE];    /* Name of lock file */
  21. static int    lockfile;        /* File descriptor of lock file */
  22.  
  23. int
  24. ttylock(ttynam)
  25.     char    *ttynam;
  26. {
  27.  
  28. #ifdef MULTITASK
  29.     sprintf(lockname, "LCK.%s", ttynam);
  30.     strcpy(lockname, munge_filename(lockname));
  31.     /* FIXME, next line is nonportable */
  32.     lockfile = open(lockname, O_RDWR|O_BINARY|O_CREAT|O_EXCL, 0444);
  33.     if (lockfile == -1) {
  34.         DEBUG(9, "Can't create tty lock %s", lockname);
  35.         /* FIXME, check for expired lock (pid not running) here. */
  36.         lockname[0] = '\0';    /* We don't hold a lock */
  37.         return FAIL;
  38.     }
  39.  
  40.     if (ourpid == 0)
  41.         ourpid = getpid();
  42.  
  43.     if (sizeof ourpid != write(lockfile, (char *)&ourpid, sizeof ourpid)) {
  44.         DEBUG(0, "Write of pid to %s failed\n", lockname);
  45.         (void) close(lockfile);
  46.         ttyunlock(ttynam);
  47.         return FAIL;
  48.     }
  49. #endif
  50.  
  51.     return SUCCESS;
  52. }
  53.  
  54. int
  55. ttyunlock()
  56. {
  57.  
  58. #ifdef MULTITASK
  59.     if (lockname[0] == '\0')
  60.         return SUCCESS;
  61.  
  62.     if (lockfile != -1 && 0 != close(lockfile)) {
  63.         DEBUG(0, "Lock file close failed\n", 0);
  64.     }
  65.     lockfile = -1;
  66.  
  67.     if (0 != remove(lockname)) {
  68.         DEBUG(0, "Removing lock file %s failed", lockname);
  69.         DEBUG(0, " with errno %d\n", errno);
  70.         lockname[0] = '\0';
  71.         return FAIL;
  72.     }
  73.     lockname[0] = '\0';
  74. #endif
  75.  
  76.     return SUCCESS;
  77. }
  78.